--[[/**
 * 我方总血量百分比 低于敌方时, 每低 N% 所有英雄增加 M% 属性  最高不超过 K%
 * [0] N
 * [1] M
 * [2] K
 * [3] 属性类型
 * [4] 属性类型
 * ... 属性类型
-- */]]

---@class BuffEffect506100 : BaseBuffEffect
BuffEffect506100 = ClientFight.CreateClass("BuffEffect506100", ClientFight.BaseBuffEffect)
local t = BuffEffect506100
t.BuffEffect506100_step = "BuffEffect506100_step";
---@param fight Fight
---@param buff FighterBuff
function t:action(fight, buff, ...)
    local target = buff.target;
    local ratio = self:calRatio(fight, target);
    local buffEffectValue = buff.buffBean.f_BuffEffectValue;
    local step = ratio / buffEffectValue[1];--得到加成倍率
    if (step < 0) then
        step = 0;
    end
    local oldStep = buff.effectParams[t.BuffEffect506100_step] or 0;
    if (oldStep == step) then
        return IBuffEffect.FAIL;
    end
    buff.effectParams[t.BuffEffect506100_step] = step;
    local addValue = step * buffEffectValue[2];
    if (addValue > buffEffectValue[3]) then
        addValue = buffEffectValue[3];
    end
    local value = addValue;
    for i = 4, table.getn(buffEffectValue) do
        local type = buffEffectValue[i];
        SkillRoleMgr.savePathFile("[" .. fight.frame .. "][EFFECT]Fighter " .. buff.target.fighterId .. " Buff"
                .. buff.modelId .. "总血量低于敌方万分之 " .. ratio .. " ,增加属性 : " .. type .. ", " .. value);
        buff.attributes.attributes[type] = value;
    end
    local fighters = target.fightTeam.fighters;
    for k, fighter in pairs(fighters) do
        local fbuff = BuffManager.getBuffByModel(fighter, buff.buffBean, buff.source, true);
        if (fighter == buff.target) then
            goto continue;
        end
        if (fighter:isDie()) then
            goto continue;
        end
        if (fighter:isSummons()) then
            goto continue;
        end
        if (fbuff == nil) then
            goto continue;
        end
        local attributes = buff.attributes.attributes;
        for i, entry in pairs(attributes) do
            buff.attributes.attributes[k] = entry;
            SkillRoleMgr.savePathFile("[" .. fight.frame .. "][EFFECT]Fighter " .. fighter.fighterId .. " Buff"
                    .. buff.modelId .. "总血量低于敌方万分之 " .. ratio .. " , 队友也增加属性 : " .. k .. ", " .. entry);
        end
        FighterAttributeManager.countFighterAttribute(fight, fighter, CalculatorTypeEnum.BUFF);
        :: continue ::
    end
    ClientFight.BaseBuffEffect.action(self, fight, buff, ...);
    return IBuffEffect.SUCCESS_ATTRI_HAS_CHANGE;
end

function t:calRatio(fight, fighter)
    local fighters = fighter.fightTeam.fighters;
    local maxHp = 0;
    local hp = 0;
    for k, _fighter in pairs(fighters) do
        if (_fighter:isSummons()) then
            goto continue1;
        end
        hp = hp + _fighter.hp;
        maxHp = maxHp + FighterAttributeManager.getFightAttr(_fighter, FighterAttributeEnum.HP);
        :: continue1 ::
    end
    local ratioSelf = Utils.fromRatio(hp / maxHp);

    fighters = fight.teams[3 - fighter.teamIndex].fighters;
    maxHp = 0;
    hp = 0;
    for k, _fighter in pairs(fighters) do
        if (_fighter:isSummons()) then
            goto continue2;
        end
        hp = hp + _fighter.hp;
        maxHp = maxHp + FighterAttributeManager.getFightAttr(_fighter, FighterAttributeEnum.HP);
        :: continue2 ::

    end
    local ratioOther = Utils.fromRatio(hp / maxHp);
    return ratioOther - ratioSelf;
end
t.New()